/***
 * * PROJECT
 * *    Logical Neighborhood Routing
 * * VERSION
 * *    $Revision: 1.2 $
 * * DATE
 * *    $Date: 2007/01/29 17:48:01 $
 * * LOG
 * *    $Log: ConjunctiveScope.java,v $
 * *    Revision 1.2  2007/01/29 17:48:01  lmottola
 * *    Added debug info in toString() method.
 * *
 * *    Revision 1.1  2006/12/14 22:26:36  lmottola
 * *    LNScope becomes an interface that includes matches().
 * *
 * *
 * *   Logical Neighborhoods Routing - Java Version
 * *                                
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU General Public License
 * *   as published by the Free Software Foundation; either version 2
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU General Public License
 * *   along with this program; if not, you may find a copy at the FSF web
 * *   site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 * *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * *   Boston, MA  02111-1307, USA
 ***/

package atag.runtime.ln;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Vector;

import atag.runtime.NodeInfo;
import atag.runtime.ln.nodeAttributes.NodeAttribute;
import atag.runtime.ln.predicates.LNPredicate;

/**
 * @author Luca Mottola <a
 *         href="mailto:mottola@elet.polimi.it">mottola@elet.polimi.it</a>
 * 
 */
public class ConjunctiveScope implements Serializable, LNScope {

	private static final byte NO_MAX_HOPS = -1;

	private Vector predicates;

	private byte maxHops;

	public ConjunctiveScope(LNPredicate[] preds) {

		this.predicates = new Vector();
		for (int i = 0; i < preds.length; i++) {
			predicates.add(preds[i]);
		}
		this.maxHops = NO_MAX_HOPS;
	}

	public ConjunctiveScope(LNPredicate[] preds, byte maxHops) {

		this.predicates = new Vector();
		for (int i = 0; i < preds.length; i++) {
			predicates.add(preds[i]);
		}
		this.maxHops = maxHops;
	}

	public boolean isHopConcerned() {
		return !(maxHops == NO_MAX_HOPS);
	}

	public byte getMaxHops() {
		return maxHops;
	}

	public boolean matches(NodeInfo node) {

		Iterator it = predicates.iterator();
		while (it.hasNext()) {
			LNPredicate p = (LNPredicate) it.next();
			if (!checkPredicate(p, node)) {
				return false;
			}
		}
		return true;
	}

	private boolean checkPredicate(LNPredicate p, NodeInfo nodeInfo) {

		Iterator it = new Vector(nodeInfo.getNodeAttributes()).iterator();
		while (it.hasNext()) {
			NodeAttribute attr = (NodeAttribute) it.next();
			if (p.matches(attr)) {
				return true;
			}
		}
		return false;
	}
	
	public String toString() {
		
		String ret = new String("Conjunctive Scope made of "+ predicates.size()+ " predicates");
		Iterator it = predicates.iterator();
		while (it.hasNext()) {
			ret = ret.concat("\n"+((LNPredicate)it.next()).toString());
		}
		
		return ret;
	}
}
